import Koa from 'koa'
import KoaRouter from 'koa-router'
import bodyParser from 'koa-bodyparser' // 引入 koa-bodyparser
import mockList from './mock/index.js'
import loginInterceptor from './interceptor/loginInterceptor.js'

const app = new Koa()
const router = new KoaRouter()
app.use(bodyParser()) // 使用 koa-bodyparser 中间件处理body请求参数
router.get('/', async ctx => {
  ctx.body = 'mock server'
})

app.use(loginInterceptor) // 注册登录拦截器,位置必须在路由之前注册

const getRes = async fn => {
  return new Promise((resolve, reject) => {
    resolve(fn())
  })
}

mockList.forEach(item => {
  router[item.method](item.url, async ctx => {
    ctx.body = await getRes(item.response.bind(null, ctx))
  })
})

app.use(router.routes())

app.listen(5001, () => {
  console.log('mock server start at http://localhost:5001')
})
